/**
 * Created by liguangsong on 16/8/2.
 */
import React, { Component, PropTypes } from 'react'
var getJson = require('../../assist/getJson');

var allPlayer = [];

var SearchView = React.createClass({
    getDefaultProps () {
        var url = 'commonallplayers?IsOnlyCurrentSeason=1&LeagueID=00&Season=2015-16';
        getJson(url, function (result) {
            allPlayer = result["resultSets"][0]["rowSet"];
        });

        return null;
    },

    addFilter(filter, index) {
        filter["name"].push(allPlayer[index][2]);
        filter['team'].push(allPlayer[index][9]);
        filter['id'].push(allPlayer[index][0]);

        return filter;
    },

    changeHandle(event) {
        var text = event.target.value;
        var filter = {
            name: [],
            team: [],
            id: []
        };
        if (allPlayer.length == 0) {
            alert('加载未完成，稍等片刻');
        }
        else {
            if (text != "") {
                for (var i = 0; i < allPlayer.length; i++) {
                    if (allPlayer[i][2].indexOf(text) != -1) {
                        filter = this.addFilter(filter, i);
                    }
                }
            }
            this.props.inputChange(filter);
        }
    },

    render() {
        return (
            <div className="searchView">
                <input className="search" onChange={ this.changeHandle } />
                <img className="search" src="app/assist/search.png" />
            </div>
        )
    }
});

export default SearchView;